
package GUI;

import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.border.BevelBorder;

/**
 * The StartPanel Of The Program
 * @author Yaniv Kalif
 */
public class StartPanel extends JPanel implements FocusListener
{
    
    public JButton _createGroup;
    public JButton _joinGroup;
    public JTextArea _joinIPText;
    public JTextArea _joinPortText;
    public JTextArea _createPortText;
    public JTextArea _createNickName;
    public JTextArea _joinNickName;
    public JTextArea _Instructions;
    Image _imagetitle;
    Image _logoPic;
    Image _3dRunPic;
    Image _3DComputerPic;
    Image _MailBoxPic;
    Image _CreatePic;
    Image _JoinPic;
    ImageIcon _CreateButtonPic;
    ImageIcon _JoinButtonPic;

    Color _TA_Color;//The Text Areas color
    /**
     * Constructor for StartPanel
     * @param action_listener - The action listener for this panel
     * @throws FileNotFoundException
     * @throws IOException
     */
    public StartPanel(ActionListener action_listener) throws FileNotFoundException, IOException
    {
         this.setLayout(null);
         //Create/Set Colors
            _TA_Color=Color.YELLOW;//new Color(152, 251, 152);//create TextArea color

         //set the  images
             _imagetitle = Toolkit.getDefaultToolkit().getImage("Pics/talk2me.gif");
             _CreatePic = Toolkit.getDefaultToolkit().getImage("Pics/Create_Group.gif");
             _JoinPic = Toolkit.getDefaultToolkit().getImage("Pics/Join_Group.gif");
             _logoPic = Toolkit.getDefaultToolkit().getImage("Pics/3D Postman.gif");
             _3dRunPic = Toolkit.getDefaultToolkit().getImage("Pics/3d_Postman_Run.gif");
             _3DComputerPic = Toolkit.getDefaultToolkit().getImage("Pics/3D_Computer.gif");
             _MailBoxPic = Toolkit.getDefaultToolkit().getImage("Pics/MailBox.gif");
             _CreateButtonPic = new ImageIcon();
             _CreateButtonPic.setImage( Toolkit.getDefaultToolkit().getImage("Pics/3D_CreateGroup.jpg"));
              _JoinButtonPic = new ImageIcon();
             _JoinButtonPic.setImage( Toolkit.getDefaultToolkit().getImage("Pics/3D_JoinGroup.jpg"));
         //create the Buttons
            _createGroup = new JButton(/*"Create ",*/_CreateButtonPic);
            _createGroup.setToolTipText("Create A New Group");
            _createGroup.setBounds(500, 350, 90,60);
            _createGroup.setBorder(new BevelBorder(BevelBorder.RAISED));
            _createGroup.addActionListener(action_listener);
            this.add(_createGroup);
            
            _joinGroup = new JButton(/*"Join an Existing Group",*/_JoinButtonPic);
            _joinGroup.setBounds(900, 350, 90, 60);
            _joinGroup.addActionListener(action_listener);
            _joinGroup.setToolTipText("Join An Existing Group");
            _joinGroup.setBorder(new BevelBorder(BevelBorder.RAISED));
            this.add(_joinGroup);

         //create the JTextArea for IP - join
            _joinIPText = new JTextArea("Enter The Group IP");
            _joinIPText.setBounds(700, 350, 200, 20);
            _joinIPText.setBackground(_TA_Color);
            _joinIPText.setBorder(new BevelBorder(BevelBorder.LOWERED));
            this.add(_joinIPText);
        //create the JTextArea for port - join
            _joinPortText = new JTextArea("Enter The Group PORT");
            _joinPortText.setBounds(700, 370, 200, 20);
            _joinPortText.setBackground(_TA_Color);
            _joinPortText.setBorder(new BevelBorder(BevelBorder.LOWERED));
            this.add(_joinPortText);
        //create the JTextArea for port - create
            _createPortText = new JTextArea("Enter A PORT you want to listen in");
            _createPortText.setBounds(300, 350, 200, 30);
            _createPortText.setBackground(_TA_Color);
            _createPortText.setBorder(new BevelBorder(BevelBorder.LOWERED));
            this.add(_createPortText);
        //create the JTextArea for NickName - create
            _createNickName = new JTextArea("Enter Your Nickname");
            _createNickName.setBounds(300, 380, 200, 30);
            _createNickName.setBackground(_TA_Color);
            _createNickName.setBorder(new BevelBorder(BevelBorder.LOWERED));
            this.add(_createNickName);
       //create the JTextArea for NickName - join
            _joinNickName = new JTextArea("Enter Your Nickname");
            _joinNickName.setBounds(700, 390, 200, 20);
            _joinNickName.setBorder(new BevelBorder(BevelBorder.LOWERED));
            _joinNickName.setBackground(_TA_Color);
            this.add(_joinNickName);
       //Create The instructions JTextArea
            _Instructions = new JTextArea();
            _Instructions.setBackground(_TA_Color);
            _Instructions.setBounds(300, 500, 700, 120);
            _Instructions.setBorder(new BevelBorder(BevelBorder.LOWERED));
            _Instructions.setEditable(false);
            Reader reader = new FileReader("Instructions.txt");
            _Instructions.read(reader, null);
            this.add(_Instructions);

            _createPortText.addFocusListener(this);
            _createNickName.addFocusListener(this);
            
            _joinPortText.addFocusListener(this);
            _joinNickName.addFocusListener(this);
            _joinIPText.addFocusListener(this);
    }

    @Override
    protected void paintComponent(Graphics g)
    {
        super.paintComponent(g);
            //Set The BG Color - in a gradient color type/////////////
                    int panelHeight = getHeight();
                    int panelWidth = getWidth();
                    GradientPaint gradientPaint = new GradientPaint( 0 , 0 , Color.ORANGE , panelWidth , panelHeight ,Color. red );
                    if( g instanceof Graphics2D )
                    {
                            Graphics2D graphics2D = (Graphics2D)g;
                            graphics2D.setPaint( gradientPaint );
                            graphics2D.fillRect( 0 , 0 , panelWidth , panelHeight );
                     }

            //Draw The Images
                    g.drawImage(_imagetitle, 350, 50, this);
                    g.drawImage(_logoPic, -70, -20,this);
                    g.drawImage(_3dRunPic, 900,50,this);
                    g.drawImage(_3DComputerPic, 1050,400, this);
                    g.drawImage(_MailBoxPic, 100,650, this);
                    g.drawImage(_CreatePic, 280, 250, this);
                    g.drawImage(_JoinPic, 700, 230, this);
                    g.setColor(Color.black);

    }

    public void focusGained(FocusEvent e) {

        if (e.getSource() == _joinIPText)
        {
            if (_joinIPText.getText().equalsIgnoreCase("Enter The Group IP"))
                _joinIPText.setText("");
        }
        else if (e.getSource() == _joinPortText)
        {
            if (_joinPortText.getText().equalsIgnoreCase("Enter The Group PORT"))
                _joinPortText.setText("");
        }
        else if (e.getSource() == _createPortText)
        {
            if (_createPortText.getText().equalsIgnoreCase("Enter A PORT you want to listen in"))
                _createPortText.setText("");
        }
        else if (e.getSource() == _createNickName)
        {
            if (_createNickName.getText().equalsIgnoreCase("Enter Your Nickname"))
                _createNickName.setText("");
        }
        else if (e.getSource() == _joinNickName)
        {
            if (_joinNickName.getText().equalsIgnoreCase("Enter Your Nickname"))
                _joinNickName.setText("");
        }
    }

    public void focusLost(FocusEvent e) {
    }
}
